package com.example.forum.services.impl;

import com.example.forum.common.AppResult;
import com.example.forum.common.ResultCode;
import com.example.forum.dao.MessageMapper;
import com.example.forum.exception.ApplicationException;
import com.example.forum.model.Message;
import com.example.forum.model.User;
import com.example.forum.services.IMessageService;
import com.example.forum.services.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class MessageServiceImpl implements IMessageService {
    @Autowired
    private IUserService iUserService;
    @Autowired
    private MessageMapper messageMapper;
    @Override
    public void create(Message message) {
        if(message==null||message.getPostUserId()==null
                ||message.getReceiveUserId()==null
                ||message.getContent()==null){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //检验接收者是否存在
        User user = iUserService.SelectById(message.getReceiveUserId());
        if(user==null||user.getDeleteState()==1){
            log.warn(ResultCode.FAILED_USER_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_USER_NOT_EXISTS));
        }

        //设置默认值
        message.setState((byte) 0);
        message.setDeleteState((byte) 0);
        message.setUpdateTime(new Date());
        message.setCreateTime(new Date());

        int i = messageMapper.insertSelective(message);
        if(i!=1){
            log.warn(ResultCode.FAILED_CREATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_CREATE));
        }

    }

    @Override
    public Integer selectUnreadCount(Long receiveUserId) {
        if(receiveUserId==null){
            log.info(ResultCode.ERROR_IS_NULL.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_IS_NULL));
        }
        Integer integer = messageMapper.selectUnreadCount(receiveUserId);
        return integer;
    }

    @Override
    public List<Message> selectByReceiveUserId(Long receiveUserId) {
        if(receiveUserId==null){
            log.info(ResultCode.ERROR_IS_NULL.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_IS_NULL));
        }
        List<Message> messages = messageMapper.selectByReceiveUserId(receiveUserId);
        return messages;
    }

    @Override
    public Message selectById(Long id) {
        if(id==null){
            log.info(ResultCode.ERROR_IS_NULL.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_IS_NULL));
        }
        Message message = messageMapper.selectByPrimaryKey(id);
        return message;
    }

    @Override
    public void updateStateById(Long id, Byte state) {
        if(id==null||state<0||state>2){
            log.info(ResultCode.ERROR_IS_NULL.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_IS_NULL));
        }

        Message message = new Message();
        message.setId(id);
        message.setState(state);
        message.setUpdateTime(new Date());

        int i = messageMapper.updateByPrimaryKeySelective(message);
        if(i!=1){
            log.warn(ResultCode.FAILED_CREATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_CREATE));
        }

    }

    @Override
    public void reply(Long repliedId, Message message) {
        // ⾮空校验
        if (repliedId == null || repliedId <= 0) {
            // 打印⽇志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        //校验repliedId对应的站内信状态
        Message existMessage = messageMapper.selectByPrimaryKey(repliedId);
        if(existMessage==null||existMessage.getDeleteState()==1){
            // 打印⽇志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        //更新状态为已恢复
        updateStateById(repliedId, (byte) 2);
        //回复的内容写入数据库
        create(message);
    }
}
